package com.mediacloud.app.newsmodule.fragment.topic


import android.graphics.Color
import android.media.AudioRecord.MetricsConstants.CHANNELS
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chinamcloud.project.shanshipin.utils.bindMagicIndicator
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.view.GalleryTransformer
import com.mediacloud.app.newsmodule.view.ScaleCircleNavigator
import com.tencent.qcloud.tim.uikit.utils.ScreenUtil
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.UIUtil


/**
 * @author zengtao Created on 2022/6/15
 * 专题带banner的 dialogFragment
 *
 */
class TopicBannerDialogFragment:DialogFragment() {

    private var banner :ViewPager2? =null
    private var ivClose: ImageView?=null
    private var list:MutableList<String> = mutableListOf()
    private var adapter:BannerAdapter?=null
    private var indicator: MagicIndicator?=null

    companion object{
        @JvmStatic
        fun getInstance(bundle: Bundle?):TopicBannerDialogFragment{
            val fragment=TopicBannerDialogFragment()
            fragment.arguments=bundle
            return  fragment
        }
    }




    override fun onStart() {
        super.onStart()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL,R.style.dialogFragmentTheme)
        dialog?.window?.let {
            it.attributes.width=ViewGroup.LayoutParams.MATCH_PARENT
            it.attributes.height=ViewGroup.LayoutParams.WRAP_CONTENT
            it.attributes.gravity=Gravity.BOTTOM
           // it.setWindowAnimations(R.style.dialogFragmentTheme)
            it.attributes.dimAmount=0.5f
        }

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_topic_banner,container,false)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        banner=view.findViewById(R.id.banner)
        ivClose=view.findViewById(R.id.ivClose)
        indicator=view.findViewById(R.id.indicator)

        initData()
        initEvent()

    }

    fun initData(){
        list.add("")
        list.add("")
        list.add("")
        list.add("")
        list.add("")
        list.add("")
        adapter=BannerAdapter()
        banner?.adapter=adapter
        val with= (ScreenUtil.getScreenWidth(context)-UIUtil.dip2px(context,210.0))/2
        banner?.getChildAt(0)
                ?.setPadding(with, 0, with, 0)
        if(banner?.getChildAt(0)!=null&&banner?.getChildAt(0) is RecyclerView){
            (banner?.getChildAt(0) as RecyclerView).clipToPadding = false
        }
        banner?.setPageTransformer(CompositePageTransformer().apply {
            addTransformer(
                GalleryTransformer()
            )
            addTransformer(MarginPageTransformer(20))

        })

        adapter?.setNewData(list)
        banner?.offscreenPageLimit=1
        banner?.setCurrentItem(1,false)
        setIndicator()


    }
    fun initEvent(){
        ivClose?.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

    private fun setIndicator() {
        indicator?.let {

            val scaleCircleNavigator = ScaleCircleNavigator(context)
            scaleCircleNavigator.setCircleCount(list.size)
            scaleCircleNavigator.setNormalCircleColor(Color.parseColor("#80ffffff"))
            //设置选中颜色
            scaleCircleNavigator.setSelectedCircleColor(Color.parseColor("#D43D3D"))
            //设置指示器间隔
            scaleCircleNavigator.setCircleSpacing(12)
            scaleCircleNavigator.setMaxRadius(7)
            scaleCircleNavigator.setMinRadius(5)


            it.navigator = scaleCircleNavigator
            it.onPageSelected(1)
            banner?.bindMagicIndicator(it)
        }


    }
    /**
     * bannerAdapter
     */
    inner class  BannerAdapter :BaseQuickAdapter<String,BaseViewHolder>(R.layout.aappfactory_item_topic_banner){
        override fun convert(helper: BaseViewHolder, item: String?) {

        }

    }
    override fun onDestroyView() {
        super.onDestroyView()
    }
}